package team.zero.lemon.module.message.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import team.zero.lemon.common.authz.annotation.AuthPassport;
import team.zero.lemon.common.exception.CustomException;
import team.zero.lemon.common.persistence.Page;
import team.zero.lemon.common.security.session.SessionContext;
import team.zero.lemon.common.security.session.model.Session;
import team.zero.lemon.common.web.BaseController;
import team.zero.lemon.module.message.entity.Message;
import team.zero.lemon.module.message.entity.MessageAction;
import team.zero.lemon.module.message.service.MessageActionService;
import team.zero.lemon.module.message.service.MessageService;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * Created by fangpingxi on 2016/11/1.
 */
@Controller
@ResponseBody
@AuthPassport
@RequestMapping(value = "/message", method = {RequestMethod.POST, RequestMethod.GET})
public class MessageController extends BaseController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageActionService messageActionService;


    @AuthPassport
    @RequestMapping("/save")
    public int SaveMessage(@RequestBody Message message) {
        return messageService.saveMessage(message);
    }

    @AuthPassport
    @RequestMapping("/query/receiving/page")
    public Page<MessageAction> getReceivingPage(@RequestBody MessageAction messageAction) {
        Session session = SessionContext.getSession();
        if (session == null) {
            messageAction.setUserId(0);
        } else {
            messageAction.setUserId(session.getId());
        }
        return messageActionService.getReceivingPage(messageAction);
    }

    @AuthPassport
    @RequestMapping("/query/sending/page")
    public Page<MessageAction> getSendingPage(@RequestBody MessageAction messageAction) {
        Session session = SessionContext.getSession();
        if (session == null) {
            messageAction.setUserId(0);
        } else {
            messageAction.setUserId(session.getId());
        }
        return messageActionService.getSendingPage(messageAction);
    }

    @AuthPassport
    @RequestMapping("/query/drafting/page")
    public Page<Message> getDraftingPage(@RequestBody Message message) {
        Session session = SessionContext.getSession();
        if (session == null) {
            message.setUserId(0);
        } else {
            message.setUserId(session.getId());
        }
        return messageService.getDraftingPage(message);
    }

    @AuthPassport
    @RequestMapping("/query/garbaging/page")
    public Page<MessageAction> getGarbagingPage(@RequestBody MessageAction messageAction) {
        Session session = SessionContext.getSession();
        if (session == null) {
            messageAction.setUserId(0);
        } else {
            messageAction.setUserId(session.getId());
        }
        return messageActionService.getGarbagingPage(messageAction);
    }


    @AuthPassport
    @RequestMapping("/query/id")
    public Message getMessageById(@RequestBody Message message) {
        return messageService.getMessageById(message.getId());
    }

    @AuthPassport
    @RequestMapping("/deleteAction/ids")
    public int deleteAction(@RequestBody MessageAction messageAction) {
        return messageActionService.deleteAction(messageAction);
    }


    @AuthPassport
    @RequestMapping("/deleteMessage/ids")
    public int deleteMessage(@RequestBody Message message) {
        return messageService.deleteMessage(message);
    }

    @AuthPassport
    @RequestMapping("/updateAction/id")
    public int updateAction(@RequestBody MessageAction messageAction) {
        return messageActionService.update(messageAction);
    }

}
